package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.customer.mapper.BankAccountMapper;
import com.jzo2o.customer.model.domain.BankAccount;

import com.jzo2o.customer.model.dto.request.BankAccountUpsertReqDTO;
import com.jzo2o.customer.service.IBankAccountService;
import com.jzo2o.mvc.utils.UserContext;
import org.springframework.stereotype.Service;

/**
* @author chy
* @description 针对表【bank_account(银行账户)】的数据库操作Service实现
* @createDate 2025-09-26 16:12:30
*/
@Service
public class BankAccountServiceImpl extends ServiceImpl<BankAccountMapper, BankAccount> implements IBankAccountService {
    /**
     * 添加银行账户
     *
     * @param reqDTO
     */
    @Override
    public void addBankAccount(BankAccountUpsertReqDTO reqDTO) {
        // 判断当前用户是否已经添加过银行账户,因为新增和修改时一个接口
        if(this.getById(UserContext.currentUserId()) != null){
            super.removeById(UserContext.currentUserId());
        }
        // 插入银行账户
        BankAccount bankAccount = new BankAccount();
        BeanUtil.copyProperties(reqDTO, bankAccount);
        // 设置用户id
        bankAccount.setId(UserContext.currentUserId());
        bankAccount.setType(UserContext.currentUser().getUserType());// 用户类型
        super.save(bankAccount);

    }

    /**
     * 获取当前用户银行账户信息
     *
     * @return
     */
    @Override
    public BankAccountUpsertReqDTO getCurrentUserBankAccount() {
        BankAccount bankAccount = super.getById(UserContext.currentUserId());
        if (null != bankAccount) {
            return BeanUtil.toBean(bankAccount, BankAccountUpsertReqDTO.class);
        }
        return null;
    }
}




